import euclid,random,ball
from pybass import *

class Brick(object):
    def __init__(self,g,x,y,c,h,v):
        self.g=g
        c+='_brick.png'
        c='bricks/'+c
        self.img=g.LoadStaticImg(c)
        self.hits=int(h)
        self.alive=None
        if v=='true':
            self.alive=True
        else:
            self.alive=False
        self.width=self.img.width
        self.height=self.img.height
        self.pos=euclid.Vector2(x,y)
        self.img.set_position(self.pos.x,self.pos.y)
        self.surprise=random.randint(1,100)

    def ChkHits(self):
        if self.g.sound: BASS_ChannelPlay(BASS_SampleGetChannel(self.g.wav_hit,0),1)
        self.g.score+=10
        self.g.hud.LabelScore()
        self.hits-=1
        if self.hits==0:
            self.alive=False
            if self.surprise<=self.g.nsurprise:
                pos=self.pos
                dest=euclid.Vector2(pos.x,pos.y+1)
                dir=pos-dest
                dir.normalize()
                b=ball.Ball(self.g,pos,dir,self.surprise,self.g.ball_size)
                self.g.balls.append(b)
            
    # Todo: Avoid checks if surrounding bricks still alive
    def ChkBallCollisions(self):
        if self.alive:
            for b in self.g.balls:
                if b.surprise==0:
                    # Top of brick
                    if b.dir.y<0:
                        if b.pos.y-b.radius<=self.pos.y+self.height//2 and b.pos.y+b.radius>self.pos.y+self.height//2:
                            x1=b.pos.x>=self.pos.x-self.width//2
                            x2=b.pos.x<=self.pos.x+self.width//2
                            if x1 and x2:
                                n=euclid.Vector2(0.0,1.0)
                                b.dir=b.dir.reflect(n)
                                b.pos.y=self.pos.y+self.height//2+b.radius
                                self.ChkHits()
                                continue
                    # Bottom of brick
                    else:
                        if b.pos.y+b.radius>=self.pos.y-self.height//2 and b.pos.y-b.radius<self.pos.y-self.height//2:
                            x1=b.pos.x>=self.pos.x-self.width//2
                            x2=b.pos.x<=self.pos.x+self.width//2
                            if x1 and x2:
                                n=euclid.Vector2(0.0,-1.0)
                                b.dir=b.dir.reflect(n)
                                b.pos.y=self.pos.y-self.height//2-b.radius
                                self.ChkHits()
                                continue
                    # Left of brick
                    if b.dir.x>0:
                        if b.pos.x+b.radius>=self.pos.x-self.width//2 and b.pos.x-b.radius<self.pos.x-self.width//2:
                            y1=b.pos.y>=self.pos.y-self.height//2
                            y2=b.pos.y<=self.pos.y+self.width//2
                            if y1 and y2:
                                n=euclid.Vector2(-1.0,0.0)
                                b.dir=b.dir.reflect(n)
                                b.pos.x=self.pos.x-self.width//2-b.radius
                                self.ChkHits()
                                continue
                    # Right of brick
                    else:
                        if b.pos.x-b.radius<=self.pos.x+self.width//2 and b.pos.x+b.radius>self.pos.x+self.width//2:
                            y1=b.pos.y>=self.pos.y-self.height//2
                            y2=b.pos.y<=self.pos.y+self.width//2
                            if y1 and y2:
                                n=euclid.Vector2(1.0,0.0)
                                b.dir=b.dir.reflect(n)
                                b.pos.x=self.pos.x+self.width//2+b.radius
                                self.ChkHits()
                                continue

    def Loop(self):
        self.ChkBallCollisions()    
